<style>

</style>
<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="用户ID" prop="user_id">
        <el-select v-model="form.user_id" placeholder="请选择用户ID">
          <el-option v-for="(item,index) in user" :label="item.name" :value="item.id" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色ID" prop="role_id">
        <el-select v-model="form.role_id" placeholder="请选择角色ID">
          <el-option v-for="(item,index) in role" :label="item.name" :value="item.id" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    name: 'role-attach',
    data () {
      return {
        form: {},
        user: [],
        role: [],
        rules: {
          role_id: [
            { required: true, message: '此项为必填' }
          ],
          user_id: [
            { required: true, message: '此项为必填' }
          ]
        }
      }
    },
    methods: {
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let params = {
              user_id: this.form.user_id,
              role_id: this.form.role_id
            }
            APP.ajax('post@user/attach', params, res => {
              if (res.data.status === 200) {
                this.$message.success('分配成功!')
              } else {
                this.$message(res.data.msg);
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    },
    mounted () {
      APP.ajax('user/list', {}, res => {
        this.user = res.data.data.data
      })
      APP.ajax('role/list', {}, res => {
        this.role = res.data.data.data
      })
    }
  }
</script>
